﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VfAccountApps.Domain.Models;

namespace VfAccountApps.Controllers
{ 
    public class BudgetController : Controller
    {
        private vfaEntities db = new vfaEntities();

        //
        // GET: /Budget/
        [Authorize]
        public ViewResult Index()
        {
            var budget_info = db.budget_info.Include("sap_info").OrderByDescending(b => b.date.Month).ThenBy(b => b.budget_info_id);

            return View(budget_info.ToList());
        }

        //
        // GET: /Budget/Details/5

        [Authorize]
        public ViewResult Details(int id)
        {
            budget_info budget_info = db.budget_info.Single(b => b.budget_info_id == id);
            return View(budget_info);
        }

        //
        // GET: /Budget/Create

        [Authorize]
        public ActionResult Create()
        {
            ViewBag.sap_info_id = new SelectList(db.sap_info, "sap_info_id", "account_sap");

            ViewBag.unit = new SelectList(new[]{
                                                new SelectListItem{ Text="Unit-1", Value="Unit-1"},
                                                new SelectListItem{ Text="Unit-2", Value="Unit-2"}
                            }, "Text", "Value");

            ViewBag.section = new SelectList(new[]{
                                                new SelectListItem{ Text="Admin", Value="Admin"},
                                                new SelectListItem{ Text="Cutting", Value="Cutting"},
                                                new SelectListItem{ Text="Sewing", Value="Sewing"},
                                                new SelectListItem{ Text="Finishing", Value="Finishing"}
                            }, "Text", "Value");
            return View();
        } 

        //
        // POST: /Budget/Create

        [Authorize]
        [HttpPost]
        public ActionResult Create(budget_info budget_info)
        {
            if (ModelState.IsValid)
            {
                var check = db.budget_info.Where(x => x.reference_no.Contains(budget_info.reference_no)).ToList();

                if (check.Count == 0)
                {
                    budget_info.reference_no = budget_info.section[0] + budget_info.reference_no;

                    budget_info.entry_date = DateTime.Now;

                    db.budget_info.AddObject(budget_info);
                    db.SaveChanges();
                }
                else
                {
                    ModelState.AddModelError("reference_no", "Duplicate Reference No");
                }
            }

            ViewBag.sap_info_id = new SelectList(db.sap_info, "sap_info_id", "account_sap", budget_info.sap_info_id);

            ViewBag.unit = new SelectList(new[]{
                                                new SelectListItem{ Text="Unit-1", Value="Unit-1"},
                                                new SelectListItem{ Text="Unit-2", Value="Unit-2"}
                            }, "Text", "Value", budget_info.unit);

            ViewBag.section = new SelectList(new[]{
                                                new SelectListItem{ Text="Admin", Value="Admin"},
                                                new SelectListItem{ Text="Cutting", Value="Cutting"},
                                                new SelectListItem{ Text="Sewing", Value="Sewing"},
                                                new SelectListItem{ Text="Finishing", Value="Finishing"}
                            }, "Text", "Value", budget_info.section);
            
            return View(budget_info);
        }
        
        //
        // GET: /Budget/Edit/5

        [Authorize]
        public ActionResult Edit(int id)
        {
            budget_info budget_info = db.budget_info.Single(b => b.budget_info_id == id);

            ViewBag.sap_info_id = new SelectList(db.sap_info, "sap_info_id", "account_sap", budget_info.sap_info_id);

            ViewBag.unit = new SelectList(new[]{
                                                new SelectListItem{ Text="Unit-1", Value="Unit-1"},
                                                new SelectListItem{ Text="Unit-2", Value="Unit-2"}
                            }, "Text", "Value", budget_info.unit);

            ViewBag.section = new SelectList(new[]{
                                                new SelectListItem{ Text="Admin", Value="Admin"},
                                                new SelectListItem{ Text="Cutting", Value="Cutting"},
                                                new SelectListItem{ Text="Sewing", Value="Sewing"},
                                                new SelectListItem{ Text="Finishing", Value="Finishing"}
                            }, "Text", "Value", budget_info.section);
            return View(budget_info);
        }

        //
        // POST: /Budget/Edit/5

        [Authorize]
        [HttpPost]
        public ActionResult Edit(budget_info budget_info)
        {
            if (ModelState.IsValid)
            {
                string tempReferenceNo = budget_info.reference_no.Remove(0, 1);

                var check = db.budget_info.Where(x => x.reference_no.Contains(tempReferenceNo) && x.budget_info_id != budget_info.budget_info_id).ToList();

                if (check.Count == 0)
                {
                    db.budget_info.Attach(budget_info);
                    db.ObjectStateManager.ChangeObjectState(budget_info, EntityState.Modified);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("reference_no", "Duplicate Reference No");
                }
            }
            ViewBag.sap_info_id = new SelectList(db.sap_info, "sap_info_id", "sap_description", budget_info.sap_info_id);
            ViewBag.unit = new SelectList(new[]{
                                                new SelectListItem{ Text="Unit-1", Value="Unit-1"},
                                                new SelectListItem{ Text="Unit-2", Value="Unit-2"}
                            }, "Text", "Value", budget_info.unit);

            ViewBag.section = new SelectList(new[]{
                                                new SelectListItem{ Text="Admin", Value="Admin"},
                                                new SelectListItem{ Text="Cutting", Value="Cutting"},
                                                new SelectListItem{ Text="Sewing", Value="Sewing"},
                                                new SelectListItem{ Text="Finishing", Value="Finishing"}
                            }, "Text", "Value", budget_info.section);
            return View(budget_info);
        }

        //
        // GET: /Budget/Delete/5

        [Authorize]
        public ActionResult Delete(int id)
        {
            budget_info budget_info = db.budget_info.Single(b => b.budget_info_id == id);
            return View(budget_info);
        }

        //
        // POST: /Budget/Delete/5

        [Authorize]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            budget_info budget_info = db.budget_info.Single(b => b.budget_info_id == id);
            db.budget_info.DeleteObject(budget_info);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}